{include file="common/header"}
<style>
    table, th, td {
        border: 1px solid #E6E6E6;
    }
</style>
<body>
<form class="layui-form" action="">
    <div class="mainBox">
        <div class="main-container">
            <div class="layui-form-item">
                <label class="layui-form-label"><font color="red">*</font>训练名称</label>
                <div class="layui-input-block">
                    <input type="text" name="train_title" class="layui-input" lay-verify="required"
                           value="{$data['train_title']??''}">
                </div>
            </div>
            <div class="layui-form-item">
                <label class="layui-form-label"><font color="red">*</font>开始时间</label>
                <div class="layui-input-block">
                    <input type="text" name="begin_time" id="begin_time" class="layui-input" lay-verify="required"
                           value="{$data['begin_time']??''}">
                </div>
            </div>
            <div class="layui-form-item">
                <label class="layui-form-label"><font color="red">*</font>结束时间</label>
                <div class="layui-input-block">
                    <input type="text" name="end_time" id="end_time" class="layui-input" lay-verify="required"
                           value="{$data['end_time']??''}">
                </div>
            </div>
            <div class="layui-form-item">
                <label class="layui-form-label"><font color="red">*</font>用户名称</label>
                <div class="layui-input-block">
                    <div type="text" id="user_id"></div>
                </div>
            </div>
        </div>
    </div>
    <div class="bottom">
        <div class="button-container" style="left: 30px">
            <input type="hidden" name="__token__" id="__token__" value="{:token()}"/>
            <input type="hidden" name="train_id" value="{$data['train_id']??''}">
            <button type="button" class="layui-btn layui-btn-normal layui-btn-sm" lay-submit="" lay-filter="save">
                <i class="layui-icon layui-icon-ok"></i>
                提交
            </button>
            <button type="reset" class="layui-btn layui-btn-primary layui-btn-sm">
                <i class="layui-icon layui-icon-refresh"></i>
                重置
            </button>
        </div>
    </div>
</form>
<script language="JavaScript" type="text/javascript" src="/static/component/pear/xm-select.js"></script>
<script language="JavaScript" type="text/javascript" src="/static/admin/js/jquery.min.js"></script>
<script>
    var demo1 = xmSelect.render({
        el: '#user_id',
        radio: true,
        tips: '搜索用户名称或手机号',
        filterable: true,
        toolbar: {show: false},
        data: [],
        name: 'user_id',
        paging: true, // 开启分页
        pageSize: 10, // 每页显示条数
        remoteSearch: true,
        pageRemote: true, // 远程分页
        remoteMethod: function (val, cb, show, pageIndex) { // pageIndex为当前页码
            $.ajax({
                url: '{$Request.root}/Train/searchUser',
                method: 'GET',
                data: {
                    keyword: val,
                    page: pageIndex, // 传递页码参数给后端
                    pageSize: 10,
                    userId: "{$data['user_id']}"
                },
                dataType: 'json',
                success: function (res) {
                    if (res.code === 0) {
                        var remoteData = [];
                        res.data.list.forEach(function (item) { // 假设返回数据包含list数组
                            remoteData.push({
                                name: item.name,
                                value: item.value,
                                selected: item.selected
                            });
                        });
                        var totalPage = Math.ceil(res.data.totalCount / 10); // 计算总页数
                        cb(remoteData, totalPage); // 回调函数第二个参数传入总页数
                    } else {
                        cb([], 0);
                    }
                },
                error: function () {
                    cb([], 0);
                }
            });
        }
    })
    layui.use(['form', 'jquery', 'common', 'laydate'], function () {
        let form = layui.form;
        let $ = layui.jquery;
        let common = layui.common;
        var laydate = layui.laydate;

        laydate.render({
            elem: '#begin_time',
            type: "date",
        });

        laydate.render({
            elem: '#end_time',
            type: "date",
        });

        form.on('submit(save)', function (data) {
            $.ajax({
                data: JSON.stringify(data.field),
                dataType: 'json',
                contentType: 'application/json',
                type: 'post',
                success: function (res) {
                    if (res.code == 200) {
                        layer.msg(res.msg, {icon: 1, time: 1000}, function () {
                            parent.layer.close(parent.layer.getFrameIndex(window.name));//关闭当前页
                            parent.layui.table.reload("dict-table");
                        });
                    } else {
                        layer.msg(res.msg, {icon: 2, time: 1000}, function () {
                            common.getToken("{$Request.root}");
                        });
                    }
                }
            })
            return false;
        });
    })
</script>
</body>
</html>